package com.sketch.service;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sketch.commom.PageResult;
import com.sketch.commom.Result;
import com.sketch.entity.PayConfig;
import com.sketch.entity.dto.PayConfigDTO;
import com.sketch.mapper.PayConfigMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class PayConfigService {
    @Resource
    private PayConfigMapper payConfigMapper;

    public Result addOrUpdate(PayConfigDTO payConfigDTO){
        PayConfig payConfig = new PayConfig();
        BeanUtils.copyProperties(payConfigDTO,payConfig);
        if(ObjectUtils.isEmpty(payConfigDTO.getId())){
            payConfigMapper.insert(payConfig);
        }else {
            payConfigMapper.updateById(payConfig);
        }

        return Result.success();
    }
    public Result list( Integer page, Integer size){
        Page<PayConfig> vipPricePage = new Page<>(page, size);
        IPage<PayConfig> result = payConfigMapper.selectPage(vipPricePage,null);
        PageResult dtoPage = new PageResult<>();
        BeanUtils.copyProperties(result,dtoPage);
        List<PayConfigDTO> list = new ArrayList<>();
        if(ObjectUtils.isEmpty(result.getRecords())){
            return Result.success(dtoPage);
        }
        result.getRecords().forEach(e->{
            PayConfigDTO payConfigDTO = new PayConfigDTO();
            BeanUtils.copyProperties(e,payConfigDTO);
            list.add(payConfigDTO);
        });
        dtoPage.setList(list);
        return Result.success().withData(dtoPage);
    }

    public Result detail(Long id){
        PayConfig vipPrice = payConfigMapper.selectById(id);
        PayConfigDTO vipPriceDTO = new PayConfigDTO();
        BeanUtils.copyProperties(vipPrice,vipPriceDTO);
        return Result.success().withData(vipPriceDTO);
    }

    public Result delete(Long id){
        payConfigMapper.deleteById(id);
        return Result.success();
    }
}
